package ltd.hanamaki.review.iotest;

import java.io.*;

public class IoTestUTF8 {

    public static void main(String[] args) throws Exception {

//        ioTestUTF8();

        ioTestUTF8Reader();
    }

    /* 设置字符编码并且将内容写出 */
    public static void ioTestUTF8() throws Exception {

        /* 保存文件的地址 */
        File file = new File("c://test.txt");
        /* 写出文件 */
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        /* 为 文件内容写出是设置 字符编码 */
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");

        /* 创建数据并写出 */
        outputStreamWriter.append("我是牛牛").append("I am niu niu");

        /* 关闭流 */
        outputStreamWriter.close();
        fileOutputStream.close();

    }


    /* 设置字符编码并且将内容写出 */
    public static void ioTestUTF8Reader() throws Exception {

        /* 保存文件的地址 */
        File file = new File("c://test.txt");
        /* 写出文件 */
        FileInputStream fileInputStream = new FileInputStream(file);
        /* 为 文件内容写出是设置 字符编码 */
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");

        StringBuffer stringBuffer = new StringBuffer();
        while (inputStreamReader.ready()) {
            stringBuffer.append((char) inputStreamReader.read());
        }

        System.out.println( stringBuffer.toString());


    }


}
